package com.csdn.leon.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.List;

import cn.trinea.android.common.util.ListUtils;

import com.csdn.leon.activity.BlogCloumnListActivity;
import com.csdn.leon.activity.FavoriteActivity;
import com.csdn.leon.activity.WatchBloggerActivity;
import com.csdn.leon.view.viewpager.AutoScrollViewpager;
import com.csdn.leon.view.viewpager.ImagePagerAdapter;
import com.csdn.leon.base.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

public class FindFragment extends BaseFragment {

	private Activity activity;
	private AutoScrollViewpager viewPager;
	private View view;
	private List<Integer> imageIdList;
	private ArrayList<ImageView> dotImagelist = new ArrayList<ImageView>();
	private LinearLayout dotLayout;

	private RelativeLayout cloumnlayout;
	private RelativeLayout whatchlayout;
    private RelativeLayout favoriteLayout;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);// 保留fragment对象
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.found_fragment, container, false);
		initView();
		initViewData();
		return view;
	}

	private void initView() {
		imageIdList = new ArrayList<Integer>();
		imageIdList.add(R.drawable.banner1);
		imageIdList.add(R.drawable.banner2);
		imageIdList.add(R.drawable.banner3);
		imageIdList.add(R.drawable.banner4);
		viewPager = (AutoScrollViewpager) view.findViewById(R.id.view_pager);
		viewPager.setAdapter(new ImagePagerAdapter(activity, imageIdList)
				.setInfiniteLoop(true).setInfiniteLoop(false));
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());

		viewPager.setInterval(3000);
		viewPager.startAutoScroll();
		viewPager.setCurrentItem(0);

		viewPager.startAutoScroll();

		dotLayout = (LinearLayout) view.findViewById(R.id.dot_layout);
		cloumnlayout = (RelativeLayout) view.findViewById(R.id.cloumn_layout);
		cloumnlayout.setOnClickListener(this);
		whatchlayout = (RelativeLayout) view.findViewById(R.id.whatchblogger_layout);
		whatchlayout.setOnClickListener(this);
		favoriteLayout = (RelativeLayout) view.findViewById(R.id.favorite_layout);
		favoriteLayout.setOnClickListener(this);
	}

	private void initViewData() {
		for (int i = 0; i < imageIdList.size(); i++) {
			ImageView dotImg = new ImageView(activity);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			params.leftMargin = 4;
			params.rightMargin = 4;
			dotImg.setImageResource(R.drawable.dot_normal);
			if (i == 0) {
				dotImg.setImageResource(R.drawable.dot_focus);
			}
			dotLayout.addView(dotImg, params);
			dotImagelist.add(dotImg);

		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cloumn_layout:
			Intent intent = new Intent(activity, BlogCloumnListActivity.class);
			startActivity(intent);
			break;
		case R.id.whatchblogger_layout:
			Intent intent1 = new Intent(activity, WatchBloggerActivity.class);
			startActivity(intent1);
			break;
		case R.id.favorite_layout:
			Intent intent2 = new Intent(activity, FavoriteActivity.class);
			startActivity(intent2);
			break;
		}
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int position) {
			for (int i = 0; i < dotImagelist.size(); i++) {
				if (i == position) {
					dotImagelist.get(i).setImageResource(R.drawable.dot_focus);
				} else {
					dotImagelist.get(i).setImageResource(R.drawable.dot_normal);
				}
			}
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		// stop auto scroll when onPause
		viewPager.stopAutoScroll();
	}

	@Override
	public void onResume() {
		super.onResume();
		viewPager.startAutoScroll();

	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
}
